<template>
  <!-- 账户管理 -->
  <div class="account-container">
    <PageCard
      title="查询条件"
      :showTitle="false"
    >
      <el-form
        :model="formData"
        label-position="right"
        size="small"
        :inline="true"
        ref="searchForm"
        label-width="100px"
      >
        <el-form-item prop="name">
          <el-input
            class="w350"
            placeholder="请输入设备类型名称"
            v-model="formData.name"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleSearch()"
          >查询</el-button>
          <el-button @click="handleReset()">重置</el-button>
        </el-form-item>
      </el-form>
    </PageCard>
    <PageCard
      title="设备类型列表"
      icon_type="list"
      :hasCreate="true"
      btnText="新增设备类型"
      @create="handleAdd()"
    >
      <PageTable
        :data="data"
        :currentPage="currentPage"
        :pageSize="pageSize"
        :total="total"
        @selectionChange="selectionChange"
        @handleChange="handleSearch"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="55"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="序号"
          type="index"
          width="160"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="设备类型"
        > </el-table-column>
        <el-table-column
          prop="code"
          label="设备类型编号"
        > </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
        >
        </el-table-column>
        <el-table-column
          label="操作"
          width="230"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              @click.stop="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              class="text-danger"
              @click.stop="handleDel(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </PageTable>
    </PageCard>
    <Child
      ref="child"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></Child>
    <ChooseRole
      ref="chooseRole"
      @getDataList="handleSearch(currentPage,pageSize)"
    ></ChooseRole>
  </div>
</template>

<script>
import PageTable from "@/components/PageTable";
import PageCard from "@/components/PageCard";
import child from "./components/child.vue";
import chooseRole from "./components/chooseRole.vue";
import * as deviceClassifyApi from "@/api/deviceClassify.js";
import { settings } from "nprogress";
export default {
  name: "Users",
  components: {
    PageTable: PageTable,
    PageCard: PageCard,
    Child: child,
    ChooseRole: chooseRole,
  },
  data() {
    return {
      formData: {
        name: "",
      },
      currentPage: 1,
      pageSize: 10,
      total: 0,
      data: [],
      roleList: [], //角色列表
      multipleTable: [], //批量选择
    };
  },
  created() {
    this.handleSearch(1, 10);
  },
  methods: {
    // 查询
    handleSearch(current, limit) {
      this.currentPage = current;
      this.pageSize = limit;
      const params = {
        ...this.formData,
        pageIndex: current || 1,
        pageSize: limit || 10,
      };
      // 调接口查询
      this.handleGetDataList(params);
    },
    //获取列表数据
    handleGetDataList(params) {
      deviceClassifyApi.deviceClassifyList(params).then((res) => {
        if (res && res.success) {
          this.data = res.data;
          this.total = res.total;
        }
      });
    },
    // 重置
    handleReset() {
      this.$refs["searchForm"].resetFields();
    },
    handleAdd() {
      this.$refs.child.showModal("add");
    },
    handleEdit(row) {
      this.$refs.child.showModal("edit", row);
    },

    //删除用户
    handleDel(item) {
      this.$confirm("此操作将永久删除该设备类型, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deviceClassifyApi.deviceClassifyRemove(item.id).then((res) => {
            if (res && res.success) {
              this.handleSearch(this.currentPage, this.pageSize);
              this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分配角色
    handleRole(row) {
      this.$refs.chooseRole.showModal("edit", row);
    },
    //重置密码
    handleResetPwd() {},
    //多选
    selectionChange(val) {
      this.multipleTable = val;
    },
  },
};
</script>

<style lang="scss" scoped>
.account-container {
  .btns {
    text-align: right;
  }
}
</style>
